package com.ternnetwork.core.accesscontroll.repository;

import java.util.List;

import com.ternnetwork.core.accesscontroll.vo.UserRoleCountVo;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.ternnetwork.persistence.repository.BaseRepository;
import com.ternnetwork.core.accesscontroll.entity.UserRole;
import com.ternnetwork.core.accesscontroll.entity.UserRoleId;

public interface UserRoleRepository extends BaseRepository<UserRole, UserRoleId> {
	
	
	@Modifying
	@Query("delete from UserRole t where t.user.id=?1")
	Integer deleteAllByUserId(Long userId);
	
	@Modifying
	@Query("delete from UserRole t where t.user.id=:userId and t.role.id not in(:roleIdList)")
	Integer deleteAllByUserIdAnRoleIdList(Long userId,List<Long> roleIdList);

	@Modifying
	@Query("delete from UserRole t where t.role.id=?1")
	Integer deleteAllByRoleId(Long roleId);

	@Query("select new com.ternnetwork.core.accesscontroll.vo.UserRoleCountVo(count (1),t.role.nameEn) from UserRole t where t.user.deleted=false group by t.role.nameEn")
	List<UserRoleCountVo> countByRoleCode();

}
